﻿using System.Collections.Concurrent;

namespace BingoServerCommon.Common
{
    /// <summary>
    /// 消息协议包管理
    /// </summary>
    public class PackageFactory
    {
        private static PackageFactory instance = null;
        private readonly ConcurrentDictionary<int, Package> _packDict = new ConcurrentDictionary<int, Package>();
        public PackageFactory()
        {

        }
        public static PackageFactory Instance => instance ?? (instance = new PackageFactory());

	    public void RegisterPackage(short mainID, Package pack)
        {
            int key = mainID ;
            _packDict.TryAdd(key, pack);
        }
        public Package NewPackage(short mainID,byte[] bytes)
        {
            int key = mainID ;
            Package pack = null;
            _packDict.TryGetValue(key, out pack);
            return pack == null ? null : pack.Clone(bytes);
        }

    }
}
